from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import urllib.error
finput = open('input.txt', 'r', encoding = 'utf-8')
while True :
text = finput.readline()
if text=='':
break
text = text.strip('\n')
urltext = urllib.parse.quote(text)
html = urllib.request.urlopen("https://tw.search.yahoo.com/search;_ylt=A8tUwYpDIQRU33UApv9r1gt.?fr2=time&ei=utf-8&fr=yfp&p="+urltext+"&btf=m").read()
soup = BeautifulSoup(html)
titlelist=[]
linklist=[]
datelist=[]
for title in soup.find_all('ol'):
for title in title.find_all('a','yschttl spt'):
if title.get('data-bns') or title.get('thmbplay'):
continue
else:
titlelist.append(title.get_text())
for link in soup.find_all('ol'):
for link in link.find_all('a','yschttl spt'):
if link.get('data-bns') or link.get('thmbplay'):
continue
else:
linklist.append(link.get('href'))
for date in soup.find_all('ol'):
for date in soup.find_all('span',id='resultTime'):
datelist.append(date.get_text())
foutput = open(text+'.txt', 'w', encoding = 'UTF-8')
i = 0
for i in range(len(titlelist)):
foutput.write(titlelist[i]+'\n')
foutput.write(linklist[i]+'\n')
foutput.write(datelist[i]+'\n')
foutput.write('----------------\n')
foutput.close()
finput.close()